package zdh.avatar.gui;

import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import zdh.avatar.AvatarConfig;
import zdh.avatar.base.*;

/**
 *
 * @author Gem Seeker
 */
public class ButtonGroups extends JPanel {

    public static String panelsPath;
    public static String buttonsPath;
    private ArrayList<JPanel> panels;
    private AvatarConfig config;
    private Clip currentClip;
    private ChildGroup childGroup;
    private MainFrame main;
    
    private final JPanel welcomePanel;
    public boolean welcomePanelDisplayed = false;

    public ButtonGroups(MainFrame main, AvatarConfig config) {
        panelsPath = config.getElementsPath() + "panels/";
        buttonsPath = config.getElementsPath() + "buttons/";
        this.main = main;
        this.config = config;
        panels = new ArrayList<>();
        initComponents();
        welcomePanel = createWelcomePanel();
    }

    private void loadPanels() {
        ElementPanel[] pPanels = ElementLoader.getElementPanels(panelsPath);
        if (pPanels != null) {
            if (pPanels.length > 0) {
                for (int i = 0; i < pPanels.length; i++) {
                    ElementPanel pp = pPanels[i];
                    JPanel panel = new JPanel();
                    panel.setName(pp.getPanelName());
                    panel.setBackground(pp.getBgColor());
                    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
                    panels.add(panel);
                }
            }
        }
    }

    private void loadButtons() {
        ElementButton[] eButtons = ElementLoader.getElementButtons(buttonsPath);
        if (eButtons != null) {
            if (eButtons.length > 0) {
                for (int i = 0; i < eButtons.length; i++) {
                    ElementButton eb = eButtons[i];

                    String[] paths = eb.getAudioPaths();
                    JButton b = null;
                    for (String s : paths) {
                        if (!new File(s).exists()) {
//                            errors.add(s);
//                            logger.error(s + "not found");
                        } else {
                            b = createButton(eb);
                            b.addActionListener(new ButtonHandler(eb, this));
                        }
                    }

                    if (panels.size() > 0) {
                        for (int j = 0; j < panels.size(); j++) {
                            if (panels.get(j).getName().equals(eb.getPanelName())) {
                                if (b != null) {
                                    panels.get(j).add(b);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    private JButton createButton(ElementButton prefButton) {
        JButton b = new JButton(prefButton.getButtonName());
        b.setBackground(prefButton.getButtonColor());
        b.setToolTipText(prefButton.getButtonName());
        b.setOpaque(true);
        b.setPreferredSize(new Dimension(150, 30));
        return b;
    }

    private void loadPane() {
        tabbedPane.removeAll();
        if (config.getDisplayWelcomePanelOption().equals("true")) {
            showWelcomePanel();
        }
        if (panels.size() > 0) {
            for (JPanel p : panels) {
                tabbedPane.addTab(Util.generateProperTabName(p.getName()), null,
                        p, p.getName());
            }
        }
    }

    public void load(ChildGroup childGroup) {
        this.childGroup = childGroup;
        reload();
    }

    public void reload() {
        System.out.println("Loading buttons");
        panels.clear();
        loadPanels();
        loadButtons();
        loadPane();
    }

    public void loadChildButtons(ElementButton button) {
        childGroup.loadButtons(button);
    }

    public void setCurrentClip(Clip clip) {
        this.currentClip = clip;
    }

    public Clip getCurrentClip() {
        return currentClip;
    }

    public int getPanelCount() {
        return tabbedPane.getTabCount();
    }

    public int getButtonCount() {
        ElementButton[] btns = ElementLoader.getElementButtons(buttonsPath);
        if (btns == null) {
            return -1;
        }
        return btns.length;
    }

    private JPanel createWelcomePanel(){
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
//        panel.setSize(tabbedPane.getWidth(), tabbedPane.getHeight());
//        panel.setBorder(new EmptyBorder(0, 0, 0, 0));
        JLabel label = new JLabel();
        URL url = getClass().getResource("welcome.png");
        ImageIcon icon = new ImageIcon(url);
        label.setIcon(icon);
        panel.add(label);
        return panel;
    }
    
    public void showWelcomePanel(){
        if(tabbedPane != null){
            tabbedPane.addTab(Util.generateProperTabName("Welcome"), welcomePanel);
            tabbedPane.setTabComponentAt(0, new ButtonTabComponent(main.getMenubar(), tabbedPane));
            welcomePanelDisplayed = true;
        }
    }
    
//    public void hideWelcomePanel(){
//        if(tabbedPane != null){
//            if(tabbedPane.getTitleAt(0).equals(Util.generateProperTabName("Welcome"))){
//                tabbedPane.remove(0);
//            }else{
//                for(int i=0; i<tabbedPane.getTabCount(); i++){
//                    if(tabbedPane.getTitleAt(i).equals(Util.generateProperTabName("Welcome"))){
//                        tabbedPane.remove(i);
//                    }
//                }
//            }
//            welcomePanelDisplayed = false;
//        }
//    }
    
    //<editor-fold defaultstate="collapsed" desc="Generated by Netbeans">
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();

        setOpaque(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
    //</editor-fold>
}
